const router = require('koa-router')()
const Logs = require('./../modules/LogSchema')
const util = require('../utils/util.js')
const log4js = require('./../utils/log4j')
router.prefix('/logs')

/** 列表 */
router.get('/list', async (ctx) => {
  const { type } = ctx.request.query
  const { page, skipIndex } = util.pager(ctx.request.query)
  let params = {}
  if (type) params.type = type
  const query = Logs.find(params)
  const list = await query.skip(skipIndex).limit(page.pageSize)
  const total = await Logs.countDocuments(params)
  ctx.body = util.success(
    {
      page: {
        ...page,
        total,
      },
      list,
    },
    '获取成功'
  )
})

/** 清空 */
router.get('/deleteAll', async (ctx) => {
  try {
    await Logs.deleteMany()
    ctx.body = util.success({}, 'ok!')
  } catch (error) {
    util.error(`清空日志错误=>${error}}`)
    ctx.body = util.fail(`清空日志错误=>${error}}`)
  }
})

module.exports = router
